# -*- coding: utf-8 -*-
import sys

sys.path.append("..")
import platform
from Base.BaseAndroidPhone import *
from Base.BaseAdb import *
from Base.BaseRunner import ParametrizedTestCase
from TestCase.aheadTest import PrivacySet
from TestCase.galleryTest import GalleryTest
from TestCase.settingsTest import SetttingsTest
from Base.BaseAppiumServer import AppiumServer
from multiprocessing import Pool
import unittest
from Base.BaseInit import init, mk_file,remove_file
from Base.BaseStatistics import countDate, writeExcel
from datetime import datetime
from Base.BaseApk import ApkInfo
import random
from Base import BaseInit
from Base.BaseLogcat import *


PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

def kill_adb():
    if platform.system() == "Windows":
        # os.popen("taskkill /f /im adb.exe")
        os.system(PATH("../app/kill5037.bat"))
    else:
        os.popen("killall adb")
    os.system("adb start-server")

def runnerPool(getDevices):
    devices_Pool = []

    for i in range(0, len(getDevices)):
        _pool = []
        _initApp = {}
        print("=====runnerPool=========")
        print(getDevices)
        _initApp["deviceName"] = getDevices[i]["devices"]
        _initApp["udid"] = getDevices[i]["devices"]
        _initApp["platformVersion"] = getPhoneInfo(devices=_initApp["deviceName"])["release"]
        _initApp["platformName"] = "android"
        _initApp["port"] = getDevices[i]["port"]
        _initApp["automationName"] = "uiautomator2"
        _initApp["systemPort"] = getDevices[i]["systemPort"]
        _initApp["app"] = getDevices[i]["app"]
        apkInfo = ApkInfo(_initApp["app"])
        _initApp["appPackage"] = apkInfo.getApkBaseInfo()[0]
        _initApp["appActivity"] = apkInfo.getApkActivity()
        _pool.append(_initApp)
        devices_Pool.append(_initApp)

    pool = Pool(len(devices_Pool))
    pool.map(runnerCaseApp, devices_Pool)
    pool.close()
    pool.join()


def runnerCaseApp(devices):
    starttime = datetime.now()
    suite = unittest.TestSuite()
    suite.addTest(ParametrizedTestCase.parametrize(PrivacySet, param=devices))#加入测试类
    # suite.addTest(ParametrizedTestCase.parametrize(GalleryTest, param=devices))
    suite.addTest(ParametrizedTestCase.parametrize(SetttingsTest, param=devices))
    unittest.TextTestRunner(verbosity=2).run(suite)
    endtime = datetime.now()
    countDate(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), str((endtime - starttime).seconds) + "秒")


if __name__ == '__main__':

    kill_adb()

    devicess = AndroidDebugBridge().attached_devices()
    if len(devicess) > 0:
        mk_file('android')
        l_devices = []
        for dev in devicess:
            app = {}
            app["devices"] = dev
            init(dev)
            app["port"] = str(random.randint(4700, 4900))
            app["bport"] = str(random.randint(4700, 4900))
            app["systemPort"] = str(random.randint(4700, 4900))
            app["app"] = BaseInit.apkPath
            l_devices.append(app)

        appium_server = AppiumServer(l_devices)
        appium_server.start_server()
        runnerPool(l_devices)
        writeExcel()
        appium_server.stop_server(l_devices)

        #删除temp文件
        remove_file(PATH("../yamls/temp.yaml"))

        # #log路径及解析
        # path = PATH("../Log/CrashInfo/Android/")
        # count = getCrashText().Count_crash(path)
        # print('crashlog解析完成，crash次数: %d' % count)
        # #中断logcat
        # # kill_adb()
    else:
        print("没有可用的安卓设备")